﻿'Imports System.ComponentModel
Imports System.ComponentModel.DataAnnotations
Imports Iesi.Collections.Generic
Imports TodoMicroSolutionPackV1.dal.dto
Imports TodoMicroSolutionPackV1.entidades.dal.dto
Imports NHibernate
Imports NHibernate.Cfg
Imports NHibernate.Criterion

Namespace facturacion.dal.dto
    Public Class comprobante
        Inherits genericDTO(Of Integer)

        Private _id As Integer
        Private _emisor As empresa
        Private _receptor As entidad
        Private _tipo_comprobante As constante
        Private _punto_venta As puntoVenta
        Private _numero_comprobante As Integer
        Private _concepto As constante
        Private _importe_total_no_gravados As Double
        Private _importe_otros_tributos As Double
        Private _importe_operaciones_exentas As Double
        Private _moneda As constante
        Private _moneda_cotizacion As Double
        Private _fecha_vto_cae As Date
        Private _fecha_comprobante As Date
        Private _fecha_vto_pago As Date
        Private _fecha_srv_desde As Date
        Private _fecha_srv_hasta As Date
        Private _observaciones As String = ""
        Private _cae As String
        Private _items As ISet(Of comprobanteItem) = New HashedSet(Of comprobanteItem)
        Private _comprobantes_asociados As ISet(Of comprobante) = New HashedSet(Of comprobante)

        '<DisplayName("ID")>
        <Display(Order:=0, Name:="ID")>
        Public Overridable Property id As Integer
            Get
                Return _id
            End Get
            Set(ByVal value As Integer)
                _id = value
            End Set
        End Property

        '<Browsable(False)>
        <Display(AutoGenerateField:=False)>
        Public Overridable Property emisor As empresa
            Get
                Return _emisor
            End Get
            Set(ByVal value As empresa)
                _emisor = value
            End Set
        End Property

        '<Browsable(False)>
        <Display(AutoGenerateField:=False)>
        Public Overridable Property receptor As entidad
            Get
                Return _receptor
            End Get
            Set(ByVal value As entidad)
                _receptor = value
            End Set
        End Property

        '<DisplayName("Razon social")>
        <Display(Order:=3, Name:="Razon social")>
        Public Overridable ReadOnly Property receptor_razon_social As String
            Get
                Return _receptor.razon_social
            End Get
        End Property

        '<Browsable(False)>
        <Display(AutoGenerateField:=False)>
        Public Overridable Property tipo_comprobante As constante
            Get
                Return _tipo_comprobante
            End Get
            Set(ByVal value As constante)
                _tipo_comprobante = value
            End Set
        End Property

        '<Browsable(False)>
        <Display(AutoGenerateField:=False)>
        Private ReadOnly Property tipoComprobanteConstantIsOk As Boolean
            Get
                Return _tipo_comprobante Is Nothing OrElse constantsUtil.isTipoComprobante(_tipo_comprobante)
            End Get
        End Property

        '<Browsable(False)>
        <Display(AutoGenerateField:=False)>
        Public Overridable Property punto_venta As puntoVenta
            Get
                Return _punto_venta
            End Get
            Set(ByVal value As puntoVenta)
                _punto_venta = value
            End Set
        End Property
        '<DisplayName("Punto de Venta")>
        <Display(Order:=1, Name:="Punto de venta")>
        Public Overridable ReadOnly Property punto_venta_numero As Integer
            Get
                Return _punto_venta.numero
            End Get
        End Property

        '<DisplayName("Numero de Comprobante")>
        <Display(Order:=2, Name:="Numero de Comprobante")>
        Public Overridable Property numero_comprobante As Integer
            Get
                Return _numero_comprobante
            End Get
            Set(ByVal value As Integer)
                _numero_comprobante = value
            End Set
        End Property

        '<Browsable(False)>
        <Display(AutoGenerateField:=False)>
        Public Overridable Property concepto As constante
            Get
                Return _concepto
            End Get
            Set(ByVal value As constante)
                _concepto = value
            End Set
        End Property

        '<Browsable(False)>
        <Display(AutoGenerateField:=False)>
        Private ReadOnly Property conceptoConstantIsOK() As Boolean
            Get
                Return _concepto Is Nothing OrElse constantsUtil.isConcepto(_concepto)
            End Get
        End Property

        '<DisplayName("Importe Total")>
        <Display(Order:=4, Name:="Importe Total")>
        Public Overridable ReadOnly Property importe_total As Double
            Get
                Return importe_neto + importe_iva105 + importe_iva21 + _importe_operaciones_exentas + _importe_otros_tributos + _importe_total_no_gravados
            End Get
        End Property


        '<DisplayName("Importe no Gravados")>
        <Display(Order:=5, Name:="Importe no Gravados")>
        Public Overridable Property importe_total_no_gravados As Double
            Get
                Return _importe_total_no_gravados
            End Get
            Set(ByVal value As Double)
                _importe_total_no_gravados = value
            End Set
        End Property


        '<DisplayName("Importe Neto")>
        <Display(Order:=6, Name:="Importe Neto")>
        Public Overridable ReadOnly Property importe_neto As Double
            Get
                Dim total As Double = 0
                For Each i In items
                    total = total + i.precio_total_neto
                Next

                Return total
            End Get
        End Property
        '<Browsable(False)>
        <Display(AutoGenerateField:=False)>
        Public Overridable ReadOnly Property importe_iva As Double
            Get
                Return importe_iva105 + importe_iva21
            End Get
        End Property
        '<DisplayName("Importe IVA 21")>
        <Display(Order:=7, Name:="Importe IVA 21")>
        Public Overridable ReadOnly Property importe_iva21 As Double
            Get
                Dim iva21 As Double = 0
                For Each i In items
                    If i.iva_valor = 21 Then
                        iva21 = iva21 + i.importe_iva_total
                    End If
                Next
                Return iva21
            End Get
        End Property
        '<Browsable(False)>
        <Display(AutoGenerateField:=False)>
        Public Overridable ReadOnly Property base_iva21 As Double
            Get
                Dim baseIva21 As Double = 0
                For Each i In items
                    If i.iva_valor = 21 Then
                        baseIva21 = baseIva21 + i.precio_neto
                    End If
                Next
                Return baseIva21
            End Get
        End Property

        '<DisplayName("Importe IVA 10.5")>
        <Display(Order:=8, Name:="Importe IVA 10.5")>
        Public Overridable ReadOnly Property importe_iva105 As Double
            Get
                Dim iva105 As Double = 0
                For Each i In items
                    If i.iva_valor = 10.5 Then
                        iva105 = iva105 + i.importe_iva_total
                    End If
                Next
                Return iva105
            End Get
        End Property
        '<Browsable(False)>
        <Display(AutoGenerateField:=False)>
        Public Overridable ReadOnly Property base_iva105 As Double
            Get
                Dim baseIva105 As Double = 0
                For Each i In items
                    If i.iva_valor = 10.5 Then
                        baseIva105 = baseIva105 + i.precio_neto
                    End If
                Next
                Return baseIva105
            End Get
        End Property

        '<DisplayName("Importe Otros tributos")>
        <Display(Order:=9, Name:="Importe Otros Tributos")>
        Public Overridable Property importe_otros_tributos As Double
            Get
                Return _importe_otros_tributos
            End Get
            Set(ByVal value As Double)
                _importe_otros_tributos = value
            End Set
        End Property

        '<DisplayName("Importe Operaciones exentas")>
        <Display(Order:=10, Name:="Importe Operaciones Exentas")>
        Public Overridable Property importe_operaciones_exentas As Double
            Get
                Return _importe_operaciones_exentas
            End Get
            Set(ByVal value As Double)
                _importe_operaciones_exentas = value
            End Set
        End Property

        '<Browsable(False)>
        <Display(AutoGenerateField:=False)>
        Public Overridable Property moneda As constante
            Get
                Return _moneda
            End Get
            Set(ByVal value As constante)
                _moneda = value
            End Set
        End Property

        '<Browsable(False)>
        <Display(AutoGenerateField:=False)>
        Private ReadOnly Property monedaConstantIsOK() As Boolean
            Get
                Return _moneda Is Nothing OrElse constantsUtil.isMoneda(_moneda)
            End Get
        End Property

        '<Browsable(False)>
        <Display(AutoGenerateField:=False)>
        Public Overridable Property moneda_cotizacion As Double
            Get
                Return _moneda_cotizacion
            End Get
            Set(ByVal value As Double)
                _moneda_cotizacion = value
            End Set
        End Property

        Public Overridable Property fecha_vto_cae As Date
            Get
                Return _fecha_vto_cae
            End Get
            Set(ByVal value As Date)
                _fecha_vto_cae = value
            End Set
        End Property

        '<DisplayName("Fecha")>
        <Display(Order:=12, Name:="Fecha")>
        Public Overridable Property fecha_comprobante As Date
            Get
                Return _fecha_comprobante
            End Get
            Set(ByVal value As Date)
                _fecha_comprobante = value
            End Set
        End Property


        '<Browsable(False)>
        <Display(AutoGenerateField:=False)>
        Public Overridable Property fecha_vto_pago As Date
            Get
                Return _fecha_vto_pago
            End Get
            Set(ByVal value As Date)
                _fecha_vto_pago = value
            End Set
        End Property


        '<Browsable(False)>
        <Display(AutoGenerateField:=False)>
        Public Overridable Property fecha_srv_desde As Date
            Get
                Return _fecha_srv_desde
            End Get
            Set(ByVal value As Date)
                _fecha_srv_desde = value
            End Set
        End Property


        '<Browsable(False)>
        <Display(AutoGenerateField:=False)>
        Public Overridable Property fecha_srv_hasta As Date
            Get
                Return _fecha_srv_hasta
            End Get
            Set(ByVal value As Date)
                _fecha_srv_hasta = value
            End Set
        End Property

        '<DisplayName("CAE")>
        <Display(Order:=11, Name:="CAE")>
        Public Overridable Property cae As String
            Get
                Return _cae
            End Get
            Set(ByVal value As String)
                _cae = value
            End Set
        End Property

        '<Browsable(False)>
        <Display(AutoGenerateField:=False)>
        Public Overridable Property items As ISet(Of comprobanteItem)
            Get
                Return _items
            End Get
            Set(ByVal value As ISet(Of comprobanteItem))
                _items = value
            End Set
        End Property
        Public Overridable Sub addItem(ByRef theItem As comprobanteItem)
            theItem.comprobante = Me
            _items.Add(theItem)
        End Sub
        <Display(AutoGenerateField:=False)>
        Public Overridable Property observaciones As String
            Get
                Return _observaciones
            End Get
            Set(ByVal value As String)
                _observaciones = value
            End Set
        End Property
        '<Browsable(False)>
        <Display(AutoGenerateField:=False)>
        Public Overridable Property comprobantes_asociados As ISet(Of comprobante)
            Get
                Return _comprobantes_asociados
            End Get
            Set(ByVal value As ISet(Of comprobante))
                _comprobantes_asociados = value
            End Set
        End Property
        Public Overridable Sub addComprobanteAsociado(ByRef theComprobanteAsociado As comprobanteItem)
            theComprobanteAsociado.comprobante = Me
            _items.Add(theComprobanteAsociado)
        End Sub


    End Class
End Namespace
